package swings.panels;

import swings.buttons.CommonButton;
import swings.frames.OperateSignFrame;
import swings.labels.ComboLabel;
import swings.labels.LimpidLabel;
import swings.others.CommonJTextField;
import utils.CommonUtils;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class CommonComboBoxPanel extends CommonPanel{

    private CommonPanel listPanel = new CommonPanel();
    private OperateSignFrame operateSignFrame;
    public CommonJTextField valueField = new CommonJTextField(25);
    public CommonButton no;
    private int x;
    private int width;
    private int height;
    private Boolean isExpand = false;
    private List<String> dataList;
    private int symbol;

    public CommonComboBoxPanel(int symbol, int x, int y, int width, int height, List<String> dataList, String defData, OperateSignFrame operateSignFrame) throws IOException {
        this.symbol = symbol;
        this.x = x;
        this.width = width;
        this.height = height + dataList.size()*30 + 50;
        this.dataList = dataList;
        this.operateSignFrame = operateSignFrame;
        this.setBounds(x, y, width, this.height);
        initListPanel();
        initBtn();
        valueField.setBounds(0,0, width-50, height);
        valueField.setFont(CommonUtils.getFont());
        valueField.setForeground(Color.WHITE);
        valueField.setOpaque(false);
        valueField.setText(defData);
        this.add(valueField);
    }

    private void initListPanel() throws IOException {
        listPanel.setBounds(0, 30, width-50, dataList.size()*30 + 20);
        listPanel.setVisible(false);
        for (int i=0;i<dataList.size();i++){
            listPanel.add(new ComboLabel(5 + i*32, width-10, dataList.get(i) ,this));
        }
        LimpidLabel bgLabel = new LimpidLabel(ImageUtils.common,0, 0, width, dataList.size()*30 + 20);
        listPanel.add(bgLabel);
        listPanel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1, true));
        this.add(listPanel);
    }

    private void initBtn(){
        CommonButton combo = new CommonButton(ImageUtils.comboImg, width - 80, 0, 30, 30);
        combo.addActionListener(new CommonComboBoxPanel.changeListPanel());
        no = new CommonButton(ImageUtils.no,width - 40,5,20,20);
        no.addActionListener(new deleteCombo());
        this.add(combo);
        this.add(no);
    }

    /**
     * hold监听器
     */
    private class changeListPanel implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (isExpand){
                listPanel.setVisible(false);
            }else {
                listPanel.setVisible(true);
            }
            isExpand = !isExpand;
        }
    }

    /**
     * hold监听器
     */
    private class deleteCombo implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            deleteCombo();
        }
    }

    private void deleteCombo(){
        operateSignFrame.removeCombo(symbol);
    }

    public void setY(int y){
        this.setBounds(x,y,width,height);
    }

    public void closeComboList(String value){
        valueField.setText(value);
        listPanel.setVisible(false);
        isExpand = !isExpand;
    }

}
